package com.sk.coin.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.coin.sys.entity.SysPrivilege;
import com.sk.coin.sys.mapper.SysPrivilegeMapper;
import com.sk.coin.sys.service.ISysPrivilegeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限配置 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-02-21
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements ISysPrivilegeService {


    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId, Long roleId) {
        // 1.查询所有该菜单下的权限
        List<SysPrivilege> sysPrivileges = lambdaQuery().eq(SysPrivilege::getMenuId, menuId).list();
        if (CollectionUtils.isEmpty(sysPrivileges)) {
            return Collections.emptyList();
        }
        // 2.当前的角色使用包含该权限的信息
        Set<Long> currentRoleSysPrivilegeIds = baseMapper.getPrivilegesByRoleId(roleId);
        for (SysPrivilege sysPrivilege : sysPrivileges) {
            if (currentRoleSysPrivilegeIds.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1);
            }
        }
        return sysPrivileges;
    }
}
